﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TileMapEditor;

namespace TileMapEditor
{
    public class GameMapData : ScriptableObject
    {
        [SerializeField]
        public string mapName = "";

        [SerializeField]
        public int mapId = 0;
        [ReadOnly]
        public int mapNameId;
        [ReadOnly]
        public int mapInfoId;
        [ReadOnly]
        public int unlockBarrierId;
        [ReadOnly]
        public int boxCount;
        [ReadOnly]
        public int bigBoxCount;
        [ReadOnly]
        public int preMapId;
        [ReadOnly]
        public int type;
        [ReadOnly]
        public bool haveFog;
        [ReadOnly]
        public bool haveSpecialSightRule;
        [ReadOnly]
        public int unFogObjId;
        [ReadOnly]
        public bool moveOnIce;
        [ReadOnly]
        public bool passByEventCount;

        [SerializeField]
        [ReadOnly]
        public Vector2Int startPos;

        [ReadOnly]
        public int startDir = 1;

        [SerializeField]
        //[ReadOnly]
        public List<GameTileData> dataList = new List<GameTileData>();

        [SerializeField]
        [ReadOnly]
        public List<GameObjDataObject> objList = new List<GameObjDataObject>();

        [SerializeField]
        [HideInInspector]
        public List<TriggerEffect> eventList = new List<TriggerEffect>();
        [SerializeField]
        public List<TriggerEffectData> eventListData = new List<TriggerEffectData>();

        [SerializeField]
        [ReadOnly]
        public List<EventCount> eventCountList = new List<EventCount>();//计数器

        public GameTileData GetTileData(Vector3Int ipos)
        {
            for (int i = 0; i < dataList.Count; i++)
            {
                GameTileData item = dataList[i];
                if (item.ipos.Equals(ipos))
                {
                    return dataList[i];
                }
            }
            return null;
        }

        public void AddData(GameTileData data)
        {
            bool isadd = true;
            for (int i = 0; i < dataList.Count; i++)
            {
                GameTileData item = dataList[i];
                if (item.ipos == data.ipos)
                {
                    isadd = false;
                    dataList[i] = data;
                    break;
                }
            }
            if (isadd)
            {
                dataList.Add(data);
                Debug.Log("添加位置：" + data.ipos.x + "," + data.ipos.y);
            }
        }

        public void UpdateData(GameTileData data)
        {
            for (int i = 0; i < dataList.Count; i++)
            {
                GameTileData item = dataList[i];
                if (item.ipos == data.ipos)
                {
                    dataList[i] = data;
                    Debug.Log("替换位置：" + data.ipos.x + "," + data.ipos.y);
                    return;
                }
            }
        }

        public void ClearData(Vector3Int ipos)
        {
            for (int i = 0; i < dataList.Count; i++)
            {
                GameTileData item = dataList[i];
                if (item.ipos == ipos)
                {
                    Debug.Log("删除位置：" + ipos.x + "," + ipos.y);
                    dataList.RemoveAt(i);
                    return;
                }
            }
        }

        public void Dump()
        {
            for (int i = 0; i < dataList.Count; i++)
            {
                GameTileData item = dataList[i];
                Debug.Log("数据单元：" + item.ipos.x + "," + item.ipos.y);
            }
            Debug.Log("数据总条数：" + dataList.Count);
        }

        public bool IsStartPos(Vector3Int pos)
        {
            return pos.x == startPos.x && pos.y == startPos.y;
        }
    }
}